package com.boarsoft.boar.soagov.com.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.BaseConfig;
import com.boarsoft.boar.dir.DirBiz;
import com.boarsoft.boar.dir.bean.DirTreeNode;
import com.boarsoft.boar.entity.DirInfo;
import com.boarsoft.boar.entity.SoaNexus;
import com.boarsoft.boar.soagov.com.ComBiz;
import com.boarsoft.boar.soagov.entity.ComInfo;
import com.boarsoft.boar.soagov.entity.ObjFaces;
import com.boarsoft.boar.soagov.face.ObjFacesBiz;
import com.boarsoft.boar.soagov.nexus.NexusBiz;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@RestController
@RequestMapping("/com")
public class ComAction {
	@Autowired
	private DirBiz dirBiz;
	@Autowired
	private ComBiz comBiz;
	@Autowired
	private ObjFacesBiz objFacesBiz;
	@Autowired
	private NexusBiz nexusBiz;

	/**
	 * 按应用ID分页查询所有的组件/服务<br/>
	 * 组件/服务只能位于某个应用下
	 * 
	 * @return
	 */
	@JsonIgnoreProperties({"lib","face"})
	@RequestMapping("/list.do")
	@Authorized(code = "com.list")
	public ReplyInfo<Object> list(String id, String appId, String faceId, String key, String orderBy, int pageNo,
			int pageSize) {
		if (Util.strIsNotEmpty(appId)) {
			PagedResult<ComInfo> pr = comBiz.listByAppId(appId, key, orderBy, pageNo, pageSize);
			return new ReplyInfo<Object>(true, pr);
		} else if (Util.strIsNotEmpty(faceId)) {
			PagedResult<ComInfo> pr = comBiz.listByFaceId(faceId, key, orderBy, pageNo, pageSize);
			return new ReplyInfo<Object>(true, pr);
		} else if (Util.strIsNotEmpty(id)) {
			PagedResult<ComInfo> pr = comBiz.listByNexus(id, key, orderBy, pageNo, pageSize);
			return new ReplyInfo<Object>(true, pr);
		} else {
			PagedResult<ComInfo> pr = comBiz.list(key, orderBy, pageNo, pageSize);
			return new ReplyInfo<Object>(true, pr);
		}
	}

	@RequestMapping("/save.do")
	@Authorized(code = "com.save")
	public ReplyInfo<Object> save(String parent, ComInfo c) {
		// 检查parent是否存在，取出path拼接
		DirInfo d = new DirInfo();
		d.setParentId(parent);
		d.setCode(c.getCode());
		d.setName(c.getName());
		// 设置这个目录在目录树上的路径
		if (BaseConfig.DIR_ROOT.equals(parent)) {
			d.setPath("/");
			d.setLevel(1);
		} else {
			DirInfo p = dirBiz.get(parent);
			d.setLevel(p.getLevel() + 1);
			if ("/".equals(p.getPath())) {
				d.setPath(String.format("%s%s", p.getPath(), p.getId()));
			} else {
				d.setPath(String.format("%s/%s", p.getPath(), p.getId()));
			}
		}
		// o.setLevel(level); // level是在add当中+1的
		// o.setChildCount(childCount); //初始值已为0
		d.setType(DirInfo.TYPE_COM);
		// d.setDesc(desc);
		ReplyInfo<Object> ro = comBiz.save(d, c);
		if (ro.isSuccess()) {
			DirTreeNode dtn = new DirTreeNode((DirInfo) ro.getData());
			return new ReplyInfo<Object>(true, JsonUtil.from(dtn));
		} else {
			return new ReplyInfo<Object>(String.valueOf(ro.getData()));
		}
	}

	@RequestMapping("/update.do")
	@Authorized(code = "com.update")
	public ReplyInfo<Object> update(ComInfo a) {
		// 只能改DirInfo
		DirInfo d = dirBiz.get(a.getId());
		// d.setCode(code);
		d.setName(a.getName());
		// d.setDesc(desc);
		// d.setPath(path); //path是由code决定
		//
		ComInfo c = comBiz.get(a.getId());
		// c = (ComInfo) BeanUtil.transfer(c, ComInfo.class);
		// c.setAppId(appId);
		// c.setCode(code);
		c.setName(a.getName());
		c.setType(a.getType());
		c.setFaceId(a.getFaceId());
		c.setImpl(a.getImpl());
		c.setVer(a.getVer());
		if (Util.strIsNotEmpty(a.getJson())) {
			c.setJson(a.getJson());
		}
		c.setStatus(a.getStatus());
		c.setMemo(a.getMemo());
		// update方法需判断code是否改变，一旦改变，需修改子节点的path
		ReplyInfo<Object> ro = comBiz.save(d, c);
		if (ro.isSuccess()) {
			DirTreeNode dtn = new DirTreeNode((DirInfo) ro.getData());
			return new ReplyInfo<Object>(true, JsonUtil.from(dtn));
		} else {
			return new ReplyInfo<Object>(String.valueOf(ro.getData()));
		}
	}

	@RequestMapping("/delete.do")
	@Authorized(code = "com.delete")
	public ReplyInfo<Object> delete(String id) {
		comBiz.delete(id);
		return ReplyInfo.SUCCESS;
	}

	@RequestMapping("/get.do")
	@Authorized(code = "com.get")
	public String get(String id) {
		ComInfo co = comBiz.get(id);
		StringBuilder sb = new StringBuilder();
		sb.append("{ \"success\": true, \"data\": ");
		sb.append(JsonUtil.from(co, "lib")).append(", \"byMe\": { \"0\": {}");
		List<SoaNexus> byLt = nexusBiz.listByMe(id);
		for (SoaNexus n : byLt) {
			sb.append(", \"").append(n.getObjId1()).append("\": ");
			sb.append(JsonUtil.from(n, "face,obj0,lib"));
		}
		sb.append(" }, \"onMe\": { \"0\": {}");
		List<SoaNexus> onLt = nexusBiz.listOnMe(id);
		for (SoaNexus n : onLt) {
			sb.append(", \"").append(n.getObjId1()).append("\": ");
			sb.append(JsonUtil.from(n, "face,obj1,lib"));
		}
		sb.append(" }, \"faces\": { \"0\": {}");
		PagedResult<ObjFaces> cfPr = objFacesBiz.list(id, "", "", "", "", 1, 9999);
		for (ObjFaces n : cfPr.getList()) {
			sb.append(", \"").append(n.getFaceId()).append("\": ");
			sb.append(JsonUtil.from(n.getFace(), "com"));
		}
		sb.append(" }}");
		return sb.toString();
	}
}